package com.middlegroup.payadmin.controller.sys;

import com.google.common.collect.Lists;
import com.middlegroup.common.annotation.AllLog;
import com.middlegroup.payadmin.controller.BaseController;
import com.middlegroup.payadmin.pojo.dto.DataGrid;
import com.middlegroup.payadmin.pojo.message.ApiResult;
import com.middlegroup.payadmin.pojo.po.Menu;
import com.middlegroup.payadmin.service.MenuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

import static com.middlegroup.common.exception.BusinessException.notFound;


/**
 * @author pc
 */
@Controller
@RequestMapping("/sys/menu")
@Api(value = "菜单管理")
@ApiIgnore
public class MenuController extends BaseController {

    private final MenuService menuService;


    @Autowired
    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @GetMapping("/list")
    public String list(DataGrid grid) {
        request.setAttribute("page", this.menuService.listMenuForDataGrid(grid));
        return "sys/menu/list";
    }

    @GetMapping(value = "/edit")
    public String edit(Integer id) {
        List<Menu> menus = new ArrayList<>();
        if (id != null) {
            Menu menu = this.menuService.selectById(id);
            if (null == menu){
                throw notFound("该数据已失效");
            }else {
                request.setAttribute("dto",menu);
            }
            if (null != menu.getPaterId()){
                request.setAttribute("menus",menuService.selectById(menu.getPaterId()));
            }
        }else {
            //如果前端没有传id，则是新增菜单页面，此时需要展示所有菜单节点
            request.setAttribute("menus",menuService.selectList(null));

        }


        return "sys/menu/edit";
    }

    @AllLog(module = "菜单管理", methods = "保存菜单", description = "添加/修改菜单信息")
    @PostMapping(value = "/save")
    @ResponseBody
    public ApiResult<String> save(Menu menu) {
        if (menu != null) {
            if (null == menu.getId()){
                if (null != menu.getPaterId()){
                    //查询父菜单信息
                    Menu parMenu = menuService.selectById(menu.getPaterId());
                    if (null != menu){
                        menu.setnLevel(parMenu.getnLevel()+1);
                        menu.setScort(parMenu.getScort()+menu.getOrdno()+",");
                        menuService.insert(menu);
                    }
                }
            }else {
                menuService.updateById(menu);
            }
        }
        return ApiResult.SUCCESS;
    }

    @AllLog(module = "菜单管理", methods = "移除菜单", description = "删除菜单信息")
    @PostMapping(value = "/remove")
    @ResponseBody
    public ApiResult<String> del(Integer[] ids) {
        Lists.newArrayList(ids).forEach(this.menuService::deleteById);
        return ApiResult.SUCCESS;
    }


}
